package com.ticket.web.controller.yanchu;

import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.domain.entity.SysDept;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.exception.base.BaseException;
import com.ticket.common.utils.Const;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.system.domain.YanchuHall;
import com.ticket.system.service.ISysDeptService;
import com.ticket.system.service.IYanchuHallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 场馆Controller
 * 
 * @date 2023-09-26
 */
@Api(description = "剧场管理")
@RestController
@RequestMapping("/system/hall")
public class YanchuHallController extends BaseController
{
    @Autowired
    private IYanchuHallService yanchuHallService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询场馆列表
     */
    @ApiOperation("查询场馆列表")
    @PreAuthorize("@ss.hasPermi('system:hall:list')")
    @GetMapping("/list")
    public TableDataInfo list(YanchuHall yanchuHall,Long child)
    {
        if (null == yanchuHall.getDeptId()){
            throw new BaseException("缺少剧院id");
        }
        SysDept dept = deptService.getDeptInfoById(yanchuHall.getDeptId());
        if (null == dept || Const.ZERO.equals(dept.getDeptType())){
            throw new BaseException("所属剧院不存在");
        }
        if (Const.ONE.equals(dept.getOperaLevel())){
            yanchuHall.setDeptId(null);
        }
        startPage();
        List<YanchuHall> list = yanchuHallService.selectYanchuHallList(yanchuHall);
        return getDataTable(list);
    }

    @ApiOperation("查询场馆列表")
    @GetMapping("/listHall/forAllUser")
    public TableDataInfo listHall(YanchuHall yanchuHall)
    {
        List<YanchuHall> list = yanchuHallService.selectYanchuHallList(yanchuHall);
        return getDataTable(list);
    }

    @ApiOperation("查询子场馆列表")
    @GetMapping("/listChildHall")
    public TableDataInfo listChildHall()
    {
        List<YanchuHall> list = yanchuHallService.selectChildHallList(getOperaId());
        return getDataTable(list);
    }

    /**
     * 导出场馆列表
     */
    @PreAuthorize("@ss.hasPermi('system:hall:export')")
    @Log(title = "场馆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YanchuHall yanchuHall)
    {
        List<YanchuHall> list = yanchuHallService.selectYanchuHallList(yanchuHall);
        ExcelUtil<YanchuHall> util = new ExcelUtil<YanchuHall>(YanchuHall.class);
        util.exportExcel(response, list, "场馆数据");
    }

    /**
     * 获取场馆详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:hall:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuHallService.selectYanchuHallById(id));
    }

    /**
     * 新增场馆
     */
    @PreAuthorize("@ss.hasPermi('system:hall:add')")
    @ApiOperation("新增剧场")
    @ApiImplicitParam(name = "yanchuHall", value = "剧场", dataType = "YanchuHall", dataTypeClass = YanchuHall.class)
    @Log(title = "剧场", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid YanchuHall yanchuHall)
    {
        return toAjax(yanchuHallService.insertYanchuHall(yanchuHall));
    }

    /**
     * 修改场馆
     */
    @PreAuthorize("@ss.hasPermi('system:hall:edit')")
    @Log(title = "场馆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YanchuHall yanchuHall)
    {
        return toAjax(yanchuHallService.updateYanchuHall(yanchuHall));
    }

    /**
     * 删除场馆
     */
    @PreAuthorize("@ss.hasPermi('system:hall:remove')")
    @Log(title = "场馆", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yanchuHallService.deleteYanchuHallByIds(ids));
    }
}
